import { setToken, getToken, removeToken } from '@/utils/auth'
import { loginAPI } from '@/apis/user'
import { TOKEN_KEY } from '@/constants/KEY'
// import asiox from 'axios'
export default {
  // 开启命名空间
  namespaced: true,
  state: {
    // 从缓存中读取token
    token: getToken() || ''
  },
  mutations: {
    // 设置token
    setToken(state, newtoken) {
      state.token = newtoken
      setToken(newtoken)
    },
    // 删除token
    removeToken(state) {
      state.token = null
      removeToken()
    },
    clearUserInfo(state) {
      state.token = ''
      removeToken(TOKEN_KEY)
    }
  },
  actions: {
    async login(context, data) {
      const token = await loginAPI(data.username, data.password)
      context.commit('setToken', token.data.token)
    }
  }
}
